home *** CD-ROM | disk | FTP | other *** search
Modula Implementation | 1997-04-23 | 9.6 KB | 272 lines | [TEXT/3PRM] |
- implementation module dialogDef;
-
- //
- // Dialog definitions
- //
-
- import StdClass;
- import StdInt, StdMisc, StdString, StdBool;
- import mac_types, quickdraw, pointer;
- import deltaFont, dialogLayout;
-
-
- :: PosOrSize = Default
- | Pixels Int Int;
-
-
- NoDefaultButton :== -3;
-
- ModalHor :== 8;
- MlessHor :== 1;
- ModalVer :== 28;
- MlessVer :== 39;
- HorMargin :== 12;
- VerMargin :== 12;
- HorSpace :== 16;
- VerSpace :== 16;
- RB :== HorMargin;
- R2 :== HorSpace;
- R1 :== VerSpace;
- N2 :== 8;
- N1 :== 8;
-
-
- // Calculate the exact layout in pixels of a DialogDef (result: left,top,width,height,ddef').
-
- CalculateDialogLayout :: !DialogMode !(DialogDef s io) -> (!Int,!Int,!Int,!Int,!DialogDef s io);
- CalculateDialogLayout mode dDef
- = (l,t, r-l,b-t, DialogHandleToDialogDef dH);
- where {
- (l,t, r,b) = GetDialogRect dH;
- dH = DialogDefToDialogHandle mode dDef;
- };
-
- GetDialogRect :: !(DialogHandle s io) -> Rect;
- GetDialogRect (DialogH _ _ _ rect _ _ _) = rect;
-
- DialogHandleToDialogDef :: !(DialogHandle s io) -> DialogDef s io;
- DialogHandleToDialogDef (DialogH id title _ _ _ items (DialogRest Command attrs defid))
- = CommandDialog id title attrs defid items;
- DialogHandleToDialogDef (DialogH id title _ _ _ [set =:DialogButton _ _ _ _ setf,
- reset=:DialogButton _ _ _ _ resetf : items]
- rest =:(DialogRest Property attrs _))
- = PropertyDialog id title attrs setf resetf items;
-
-
- // Transform NoticeDefs and DialogDefs into the internally used structure.
-
- NoticeDefToNoticeHandle :: !NoticeDef -> NoticeHandle s io;
- NoticeDefToNoticeHandle (Notice lines defbut buttons)
- | string == "" = NoticeH rect buts;
- = NoticeH rect [Head buts, text : Tail buts];
- where {
- rect = CenterDialogRect ModalVer (0,0,wid,hgt);
- buts = ShiftNoticeButtons (bwid - twid) buts`;
- (bwid,buts`) = NoticeButsToDialogButs (N2 + twid) btop [defbut:buttons];
- text = StaticText (-1) (ItemBox N2 N1 (twid + 4) thgt) string;
- (twid,thgt,string)= NoticeLinesToText lines;
- wid = N2 + ( Max twid bwid + N2);
- hgt = btop + (ButHgt + N1);
- btop = N1 + (thgt + N2);
- };
-
- /* NB: The functions ChangeSetFunction and ChangeResetFunction (deltaDialog) depend on the
- fact that the id's of the Set and Reset button are -1 resp. -2 !!! */
-
- DialogDefToDialogHandle :: !DialogMode !(DialogDef s io) -> DialogHandle s io;
- DialogDefToDialogHandle mode (PropertyDialog id title attrs set reset items)
- = DialogH id title Modeless rect [] [setbut,resbut:items`] rest;
- where {
- rect = ExtendPropertyRect pos (CalcDialogRect MlessHor MlessVer w h pos size items`);
- setbut = DialogButton (-1) setbox "Apply" Able set;
- resbut = DialogButton (-2) resbox "Cancel" Able reset;
- (w,h,items`)= CalcItemBoxes margins spaces items;
- rest = DialogRest Property attrs (-1);
- pos = AttrPosition attrs;
- size = AttrSize attrs;
- margins = AttrMargins attrs;
- spaces = AttrItemSpaces attrs;
- setbox = ItemBox l t MinButWid ButHgt;
- resbox = ItemBox (l-(MinButWid+hs)) t MinButWid ButHgt;
- wid = rr - rl;
- hgt = rb - rt; (rl,rt,rr,rb) = rect;
- (hm,vm) = margins;
- (hs,vs) = spaces;
- l = wid - (MinButWid + hm);
- t = hgt - (ButHgt + vm);
- };
- DialogDefToDialogHandle mode (CommandDialog id title attrs defid items)
- = DialogH id title mode rect [] items` rest;
- where {
- rect = CalcDialogRect hofs vofs w h pos size items`;
- (w,h,items`)= CalcItemBoxes margins spaces items;
- rest = DialogRest Command attrs (CheckDefaultItemId defid items items);
- (hofs,vofs) = DialogModeToOffset mode;
- pos = AttrPosition attrs;
- size = AttrSize attrs;
- margins = AttrMargins attrs;
- spaces = AttrItemSpaces attrs;
- };
-
-
- // Retrieve position, size, margins and items spaces from the attribute list.
-
- AttrPosition :: ![DialogAttribute] -> PosOrSize;
- AttrPosition [DialogPos hor ver : _] = Pixels (HorMeasureToPixels hor) (VerMeasureToPixels ver);
- AttrPosition [_ : rest] = AttrPosition rest;
- AttrPosition _ = Default;
-
- AttrSize :: ![DialogAttribute] -> PosOrSize;
- AttrSize [DialogSize wid hgt : _] = Pixels (HorMeasureToPixels wid) (VerMeasureToPixels hgt);
- AttrSize [_ : rest] = AttrSize rest;
- AttrSize _ = Default;
-
- AttrMargins :: ![DialogAttribute] -> (!Int,!Int);
- AttrMargins [DialogMargin wid hgt : rest] = (HorMeasureToPixels wid, VerMeasureToPixels hgt);
- AttrMargins [_ : rest] = AttrMargins rest;
- AttrMargins _ = (HorMargin, VerMargin);
-
- AttrItemSpaces :: ![DialogAttribute] -> (!Int,!Int);
- AttrItemSpaces [ItemSpace wid hgt : _] = (HorMeasureToPixels wid, VerMeasureToPixels hgt);
- AttrItemSpaces [_ : rest] = AttrItemSpaces rest;
- AttrItemSpaces _ = (HorSpace, VerSpace);
-
-
- // CheckDefaultItemId checks whether the default item is indeed a button.
-
- CheckDefaultItemId :: !DialogItemId ![DialogItem s io] ![DialogItem s io] -> DialogItemId;
- CheckDefaultItemId defid [DialogButton id ps tt ab bf : rest] items
- | defid == id = defid;
- = CheckDefaultItemId defid rest items;
- CheckDefaultItemId defid [DialogIconButton id ps pd il ab bf : rest] items
- | defid == id = defid;
- = CheckDefaultItemId defid rest items;
- CheckDefaultItemId defid [item : rest] items = CheckDefaultItemId defid rest items;
- CheckDefaultItemId defid [] items = GetIdOfFirstButton items;
-
- GetIdOfFirstButton :: ![DialogItem s io] -> DialogItemId;
- GetIdOfFirstButton [DialogButton id ps tt ab bf : rest] = id;
- GetIdOfFirstButton [DialogIconButton id ps pd il ab bf : rest] = id;
- GetIdOfFirstButton [item : rest] = GetIdOfFirstButton rest;
- GetIdOfFirstButton [] = NoDefaultButton;
-
-
- // Calculate (left,top,right,bottom) of a dialog.
-
- CalcDialogRect :: !Int !Int !Int !Int !PosOrSize !PosOrSize ![DialogItem s io] -> Rect;
- CalcDialogRect hofs vofs w h Default size items
- = CenterDialogRect vofs (0,0, wid,hgt);
- where {
- (wid, hgt) = CalcDialogSize w h size;
- };
- CalcDialogRect hofs vofs w h (Pixels hpos vpos) size items
- | rl >= sl && rt >= st && rr <= sr && rb <= sb = (rl,rt,rr,rb);
- = CenterDialogRect vofs (rl,rt,rr,rb);
- where {
- (sl`,st`, sr,sb, _) = QScreenRect NewToolbox;
- sl = sl` + hofs;
- st = st` + vofs;
- rl = sl + hpos;
- rt = st + vpos;
- rr = rl + wid;
- rb = rt + hgt;
- (wid, hgt) = CalcDialogSize w h size;
- };
-
- CalcDialogSize :: !Int !Int !PosOrSize -> (!Int,!Int);
- CalcDialogSize w h Default = (w, h);
- CalcDialogSize _ _ (Pixels w h) = (w, h);
-
-
- // Center a dialog on the screen.
-
- CenterDialogRect :: !Int !Rect -> Rect;
- CenterDialogRect offs (rl,rt,rr,rb)
- | leftvis && topvis = (l,t, r, b);
- | leftvis = (l,st, r, st+b-t);
- | topvis = (sl,t, sl+r-l, b);
- = (sl,st, sl+r-l, st+b-t);
- where {
- l = midh - rwid / 2; r = l + rwid;
- t = st + (shgt - rhgt) / 3; b = t + rhgt;
- rwid = rr - rl; rhgt = rb - rt;
- midh = sl + (sr - sl) / 2; shgt = sb - st;
- leftvis = l >= sl; topvis = t >= st;
- st = st` + offs; (sl,st`,sr,sb,_)= QScreenRect NewToolbox;
- };
-
- /* A PropertyDialog has two predefined buttons for which room must be
- reserved in the dialog's rectangle.
- */
-
- ExtendPropertyRect :: !PosOrSize !Rect -> Rect;
- ExtendPropertyRect Default (rl,rt, rr,rb)
- | rr - rl >= bwid = CenterDialogRect MlessVer (rl,rt, rr, rb+R1+ButHgt);
- = CenterDialogRect MlessVer (rl,rt, rl+bwid,rb+R1+ButHgt);
- where {
- bwid = R2 + but + but;
- but = MinButWid + R2;
- };
- ExtendPropertyRect position (rl,rt, rr,rb)
- | rr - rl >= bwid = (rl,rt, rr, rb+R1+ButHgt);
- = (rl,rt, rl+bwid,rb+R1+ButHgt);
- where {
- bwid = R2 + but + but;
- but = MinButWid + R2;
- };
-
- /* A modal dialog has no title bar. Therefore its vertical screen offset is
- slightly smaller.
- */
-
- DialogModeToOffset :: !DialogMode -> (!Int,!Int);
- DialogModeToOffset Modal = (ModalHor,ModalVer);
- DialogModeToOffset Modeless = (MlessHor,MlessVer);
-
-
- // Functions to transform Notice attributes into dialog items.
-
- NoticeLinesToText :: ![String] -> (!Int,!Int,!String);
- NoticeLinesToText lines = MakeNoticeText 0 0 "" DialogFont lines;
-
- MakeNoticeText :: !Int !Int !String !Font ![String] -> (!Int,!Int,!String);
- MakeNoticeText wid hgt text dfont [line]
- = (Max wid (FontStringWidth line dfont), hgt + 4 + LineHeight, text +++ line);
- MakeNoticeText wid hgt text dfont [line:lines]
- = MakeNoticeText (Max wid linewid) (hgt + LineHeight) (text +++ line +++ "\n" ) dfont lines;
- where {
- linewid= FontStringWidth line dfont;
- };
- MakeNoticeText wid hgt text dfont [] = (wid,hgt,text);
-
- NoticeButsToDialogButs :: !Int !Int ![NoticeButtonDef] -> (!Int,![DialogItem s io]);
- NoticeButsToDialogButs rgt top nbuts = MakeNoticeButtons 0 [] DialogFont rgt top nbuts;
-
- MakeNoticeButtons :: !Int ![DialogItem s io] !Font !Int !Int ![NoticeButtonDef]
- -> (!Int,![DialogItem s io]);
- MakeNoticeButtons wid dbuts dfont rgt top [NoticeButton id title : nbuts]
- = MakeNoticeButtons (wid + bwid`) [dbut:dbuts] dfont (rgt - bwid`) top nbuts;
- where {
- dbut = DialogButton id ibox title Able NButFunc;
- ibox = ItemBox (rgt - bwid) top bwid ButHgt;
- bwid = Max MinButWid (R1 + FontStringWidth title dfont );
- bwid`= bwid + R2;
- };
- MakeNoticeButtons wid dbuts dfont rgt top [] = (wid - R2,dbuts);
-
- // NButFunc :: DialogInfo * s * io -> (*s, *io);
- NButFunc dlog s io = (s,io);
-
- ShiftNoticeButtons :: !Int ![DialogItem s io] -> [DialogItem s io];
- ShiftNoticeButtons offs items
- | offs <= 0 = Reverse items [];
- = ShiftButtons [] offs items;
-
- ShiftButtons :: ![DialogItem s io] !Int ![DialogItem s io] -> [DialogItem s io];
- ShiftButtons sbuts offs [DialogButton i (ItemBox lft top wid hgt) t s f : buts]
- = ShiftButtons [sbut:sbuts] offs buts;
- where {
- sbut= DialogButton i (ItemBox (lft + offs) top wid hgt) t s f;
- };
- ShiftButtons sbuts offs [] = sbuts;
-